/*
Package service
@Time : 2022/12/21 21:20
@Author : 董胜烨
@File : salary
@Software: GoLand
@note:
*/
package service

import (
	"employee/model"
	"fmt"
	"log"
	"strconv"
)

type Salary struct {
	ID         int64  `json:"id"`
	EmployeeID int64  `json:"employee_id"`
	Date       string `json:"salary_date"`

	AccumulationFund  float64 `json:"accumulation_fund"`
	InsuranceExpenses float64 `json:"insurance_expenses"`

	BaseSalary float64 `json:"base_salary"`

	AttendanceAllowance float64 `json:"attendance_allowance"`
	OvertimeAllowance   float64 `json:"overtime_allowance"`

	OffDays int `json:"off_days"`
}

func (s *Salary) CalcAttAndBaseSalary() {
	//读取正常考勤与当月总天数
	sqlStr := fmt.Sprintf("SELECT * FROM month_full_attendance WHERE 员工编号='%d' AND 日期 ='%s'",
		s.EmployeeID, s.Date)
	res := model.QueryAll(sqlStr)
	//读出失败 直接返回
	if res == nil {
		log.Println("当月未考勤")
		return
	}
	normalDays, _ := strconv.Atoi(res[0]["正常考勤天数"])
	allDays, _ := strconv.Atoi(res[0]["当月总天数"])
	//读取未考勤天数
	sqlStr = fmt.Sprintf("SELECT * FROM month_no_attendance WHERE 员工编号='%d' AND 日期 ='%s'",
		s.EmployeeID, s.Date)
	res = model.QueryAll(sqlStr)
	//读出
	var noAttDays int
	if res == nil {
		noAttDays = 0
	} else {
		noAttDays, _ = strconv.Atoi(res[0]["未考勤天数"])
	}

	e := Employee{ID: s.EmployeeID}
	res = e.QueryOne()

	salaryDailyStr := res[0]["salary_daily"]
	salaryDaily, err := strconv.ParseFloat(salaryDailyStr, 64)
	if err != nil {
		log.Println("获取日薪失败", err)
	}
	log.Println("考勤天数", normalDays, "未考勤天数", noAttDays, "总天数", allDays, "基本工资", salaryDaily)
	//计算基本工资
	s.BaseSalary = float64(normalDays-noAttDays) * salaryDaily
	//计算全勤奖
	if normalDays != allDays-s.OffDays {
		s.AttendanceAllowance = 0 //如果考勤天数不满足当月总天数，那么没有全勤奖（一个月歇 offDay 天）
	}

}

func (s *Salary) CalcOverSalary() {
	fmt.Println(s)
	sqlStr := fmt.Sprintf("SELECT * FROM month_overtime WHERE 员工编号='%d'AND 日期='%s'", s.EmployeeID, s.Date)
	res := model.QueryAll(sqlStr)
	if res == nil {
		s.OvertimeAllowance = 0
		return
	}
	log.Println(res[0])
	s.OvertimeAllowance, _ = strconv.ParseFloat(res[0]["总加班费"], 64)
}

func (s *Salary) Insert() bool {
	s.CalcAttAndBaseSalary()
	s.CalcOverSalary()
	sqlStr := "INSERT INTO salary (employee_id, salary_date, base_salary, attendance_allowance, accumulation_fund, overtime_allowance, insurance_expenses,total_salary) VALUES (?,?,?,?,?,?,?,salary.base_salary+salary.attendance_allowance+salary.overtime_allowance-salary.insurance_expenses-salary.accumulation_fund)"
	insertId := model.Insert(sqlStr, s.EmployeeID, s.Date, s.BaseSalary, s.AttendanceAllowance, s.AccumulationFund, s.OvertimeAllowance, s.InsuranceExpenses)
	if insertId == 0 {
		return false
	}
	s.ID = insertId
	return true
}
func (s *Salary) Update() bool {
	s.CalcAttAndBaseSalary()
	s.CalcOverSalary()
	sqlStr := "UPDATE salary SET employee_id=?,salary_date=?,base_salary=?,attendance_allowance=?,accumulation_fund=?,overtime_allowance=?,insurance_expenses=?,total_salary=salary.base_salary+salary.attendance_allowance+salary.overtime_allowance-salary.insurance_expenses-salary.accumulation_fund WHERE id=?"
	return model.Update(sqlStr, s.EmployeeID, s.Date, s.BaseSalary, s.AttendanceAllowance, s.AccumulationFund, s.OvertimeAllowance, s.InsuranceExpenses, s.ID)
}
func (s Salary) Delete() bool {
	sqlStr := "DELETE FROM salary WHERE id=?"
	return model.Delete(sqlStr, s.ID)
}

func (s Salary) Query() []map[string]string {
	sqlStr := fmt.Sprintf("SELECT * FROM salary WHERE employee_id='%d'", s.EmployeeID)
	res := model.QueryAll(sqlStr)
	return res
}
